package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.zhgd.util.PageUtils;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.NLaboratoryDuctilityBo;
import com.ruoyi.zhgd.domain.vo.NLaboratoryDuctilityVo;
import com.ruoyi.zhgd.domain.NLaboratoryDuctility;
import com.ruoyi.zhgd.mapper.NLaboratoryDuctilityMapper;
import com.ruoyi.zhgd.service.INLaboratoryDuctilityService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 延度试验Service业务层处理
 *
 * @author zyb
 * @date 2023-09-11
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class NLaboratoryDuctilityServiceImpl implements INLaboratoryDuctilityService {

    private final NLaboratoryDuctilityMapper baseMapper;

    /**
     * 查询延度试验
     */
    @Override
    public NLaboratoryDuctilityVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询延度试验列表
     */
    @Override
    public TableDataInfo<NLaboratoryDuctilityVo> queryPageList(NLaboratoryDuctilityBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NLaboratoryDuctility> lqw = buildQueryWrapper(bo);
        Page<NLaboratoryDuctilityVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询延度试验列表
     */
    @Override
    public List<NLaboratoryDuctilityVo> queryList(NLaboratoryDuctilityBo bo) {
        LambdaQueryWrapper<NLaboratoryDuctility> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NLaboratoryDuctility> buildQueryWrapper(NLaboratoryDuctilityBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NLaboratoryDuctility> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getBaseCode()), NLaboratoryDuctility::getBaseCode, bo.getBaseCode());
        lqw.eq(StringUtils.isNotBlank(bo.getDeviceCode()), NLaboratoryDuctility::getDeviceCode, bo.getDeviceCode());
        lqw.like(StringUtils.isNotBlank(bo.getProjectName()), NLaboratoryDuctility::getProjectName, bo.getProjectName());
        lqw.eq(StringUtils.isNotBlank(bo.getConstructionSite()), NLaboratoryDuctility::getConstructionSite, bo.getConstructionSite());
        lqw.eq(StringUtils.isNotBlank(bo.getSampleNo()), NLaboratoryDuctility::getSampleNo, bo.getSampleNo());
        lqw.like(StringUtils.isNotBlank(bo.getSampleName()), NLaboratoryDuctility::getSampleName, bo.getSampleName());
        lqw.eq(bo.getStartTime() != null, NLaboratoryDuctility::getStartTime, bo.getStartTime());
        lqw.eq(bo.getEndTime() != null, NLaboratoryDuctility::getEndTime, bo.getEndTime());
        lqw.eq(bo.getValue1() != null, NLaboratoryDuctility::getValue1, bo.getValue1());
        lqw.eq(bo.getValue2() != null, NLaboratoryDuctility::getValue2, bo.getValue2());
        lqw.eq(bo.getValue3() != null, NLaboratoryDuctility::getValue3, bo.getValue3());
        lqw.eq(bo.getAverage() != null, NLaboratoryDuctility::getAverage, bo.getAverage());
        lqw.eq(StringUtils.isNotBlank(bo.getTemperature()), NLaboratoryDuctility::getTemperature, bo.getTemperature());
        lqw.eq(StringUtils.isNotBlank(bo.getMinValue()), NLaboratoryDuctility::getMinValue, bo.getMinValue());
        lqw.eq(StringUtils.isNotBlank(bo.getStandardTemp()), NLaboratoryDuctility::getStandardTemp, bo.getStandardTemp());
        lqw.eq(StringUtils.isNotBlank(bo.getIsQualified()), NLaboratoryDuctility::getIsQualified, bo.getIsQualified());
        lqw.eq(bo.getInsertTime() != null, NLaboratoryDuctility::getInsertTime, bo.getInsertTime());
        return lqw;
    }

    /**
     * 新增延度试验
     */
    @Override
    public Boolean insertByBo(NLaboratoryDuctilityBo bo) {
        NLaboratoryDuctility add = BeanUtil.toBean(bo, NLaboratoryDuctility.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改延度试验
     */
    @Override
    public Boolean updateByBo(NLaboratoryDuctilityBo bo) {
        NLaboratoryDuctility update = BeanUtil.toBean(bo, NLaboratoryDuctility.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NLaboratoryDuctility entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除延度试验
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public PageUtils ductilityList(Page page, Map<String, Object> params) {
        List<Map> mapList = baseMapper.ductilityList(page, params);
        return new PageUtils(page.setRecords(mapList));
    }
}
